package com.wen.cim.route.service.impl;

import com.wen.cim.route.api.req.RegisterInfoReqVO;
import com.wen.cim.route.api.res.RegisterInfoResVO;
import com.wen.cim.route.service.AccountService;
import okhttp3.OkHttpClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import static com.wen.cim.route.api.constant.Constant.ACCOUNT_PREFIX;

/**
 * @author 文洪武
 * @title: AccountServiceRedisImpl
 * @projectName cim-master
 * @description: TODO
 * @date 2021/11/2422:24
 */

@Service
public class AccountServiceRedisImpl implements AccountService {

    // 操作redis
    @Autowired
    private RedisTemplate<String, String> redisTemplate;

   /* @Autowired
    private OkHttpClient okHttpClient;
*/
    @Override
    public RegisterInfoResVO register(RegisterInfoResVO info) throws Exception {

        String key = ACCOUNT_PREFIX + info.getUserId();
        // 查询redis用户名是否存在，因为用户名和id分别都存一遍，所以查询username和id都可以
        String name = redisTemplate.opsForValue().get(info.getUserName());
        // 因为保存用户名和id都存一遍
        if (null == name) {
            redisTemplate.opsForValue().set(key, info.getUserName());
            redisTemplate.opsForValue().set(info.getUserName(), key);

        } else {
            long userId = Long.parseLong(name.split(":")[1]);
            info.setUserId(userId);
            info.setUserName(info.getUserName());
        }

        return info;
    }
}
